***Replication Do File for "A Ticking Time Bomb: Restrictions on Abortion Rights and Physical Integrity Rights"
*Nazli Avdan, Amanda Murdie, and Victor Asal"
*Date: 5/16/2024
*Please reach out to murdie@uga.edu with any questions 


**This folder just replicates the maps shown in the manuscript

clear 
*remember to set your working directory to where the replication dataset is 
cd "C:\Users\murdie\Dropbox\ISA 2023 Naz Victor Abortion\Conditional Acceptance Stage 2024\Replication Data for A Ticking Time Bomb"

use ReplicationDataset.dta, replace
tsset ccode year 


keep ccode year cai_cai1 cai_cai2 BackwardsAbortioncai1 BackwardsAbortioncai2 Largerthanorequal2dropcai1 Largerthan2SDdropcai2 


gen backslidingall = BackwardsAbortioncai1  + BackwardsAbortioncai2 +  Largerthanorequal2dropcai1 + Largerthan2SDdropcai2 

by ccode: egen totalbacksliding = sum(backslidingall)
by ccode: egen meancai1 = mean(cai_cai1)
by ccode: egen meancai2 = mean(cai_cai2)
by ccode: egen totalBackwardsAbortioncai1 = sum(BackwardsAbortioncai1)
by ccode: egen totalBackwardsAbortioncai2 = sum(BackwardsAbortioncai2)



keep if year==2015

keep if totalbacksliding!=.


		ssc install kountry, replace
		kountry ccode, from(cown) to(iso3n)
		rename _ISO3N iso3n
	kountry ccode, from(cown)
	replace iso3n = 643 if NAMES_STD=="Russia"
	replace iso3n = 729 if NAMES_STD=="Sudan"

		drop if iso3n==.
		sort iso3n
save map.dta, replace 


		*2. Now, get shapefiles that are at the correct (country) level and change those into Stata files 

		*There are lots of great places to download free shapefiles online, I use: https://www.naturalearthdata.com/, I used these files https://www.naturalearthdata.com/downloads/10m-cultural-vectors/  

		*set your working directory to where those unzipped shape files are

		cd "C:\Users\murdie\Dropbox\ISA 2023 Naz Victor Abortion\Conditional Acceptance Stage 2024\Replication Data for A Ticking Time Bomb\ne_10m_admin_0_countries"

		*3: Have Stata change your shapefiles into Stata data files
		
		ssc install shp2dta, replace
		
		shp2dta using ne_10m_admin_0_countries, data(worlddata) coor(worldcoor) genid(id)
		
		*this will create two files - a worlddata.dta file and a worldcoor.dta file from the .dbf and .shp shapefiles in that folder you downloaded 

		*let's take a look at the data real quick, just so you know what these coordinate and attribute files look like
		
		*coordinates (this is what gives the lines and shapes to draw the map)
		use worldcoor.dta, clear
		browse
		
		*attribute file (this is what we'll want to merge the happiness data with)
		*we need to find (or create) the variable that we will merge on (this would be the ISO 3-numeric codes)
		*we must relabel this as ccode (like we had in our pressfreedom dataset)
		use worlddata.dta, clear
		browse

		rename ISO_N3 iso3n
		*make sure it is a numeric variable
		destring iso3n, replace
		save "worlddata.dta", replace

		sort iso3n

		*4: merge the attribute data you are intested in working with: merge one to one on ccodes 
		*set working directory back to the class folder
		use worlddata.dta, clear
		sort iso3n

		cd "C:\Users\murdie\Dropbox\ISA 2023 Naz Victor Abortion\Conditional Acceptance Stage 2024\Replication Data for A Ticking Time Bomb"
		*merge
		merge iso3n using "map.dta"
		*now, reset the working directory back to the shapefile folder 
		cd "C:\Users\murdie\Dropbox\ISA 2023 Naz Victor Abortion\Conditional Acceptance Stage 2024\Replication Data for A Ticking Time Bomb\ne_10m_admin_0_countries"

		*5: draw map, the id is the code in the shapefiles that connects the coordinates to the attributes 

		*first, you can easily draw a blank map
		spmap using "worldcoor.dta", id(id)
		
		*heat map, quartiles are the default 
		spmap meancai1 using  "worldcoor.dta", id(id) fcolor(Reds) 

 
*colorblind friendly colors
colorpalette viridis, n(11) nograph reverse

local colors `r(p)'
* fcolor("`colors'") *

spmap meancai2 using  "worldcoor.dta" if ADMIN!="Antarctica", id(id) fcolor("`colors'") clmethod(custom) clbreaks(0 .1 .2 .3  .4  .5 .6 .7 .8 .9 1) 

		
colorpalette viridis, n(6) nograph reverse

local colors `r(p)'	
spmap totalBackwardsAbortioncai2 using  "worldcoor.dta" if ADMIN!="Antarctica", id(id) fcolor("`colors'")  clmethod(custom) clbreaks(0 1 2 3 4 5 ) 

		
		*There are more than 15 Color palettes for color blindness from Martin Krzywinski and et al.

*See also GETTING INTO VISUALIZATION OF LARGE BIOLOGICAL DATA SETS and "Controversial Color Use on Maps"(Brewer, 1997).
*https://cran.r-project.org/web/packages/colorBlindness/vignettes/colorBlindness.html
		spmap meancai2 using  "worldcoor.dta" if ADMIN!="Antarctica", id(id) fcolor(Paired)  clmethod(custom) clbreaks(0 .1 .2 .3  .4  .5 .6 .7 .8 .9 1) 
		
		spmap totalBackwardsAbortioncai2 using  "worldcoor.dta" if ADMIN!="Antarctica", id(id) fcolor(Paired)  clmethod(custom) clbreaks(0 1 2 3 4 5 ) 
				


